function getURLParams(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return unescape(r[2]);
	}
	return null; 
}

// 查找国家信息
 function findCountry(){
	$("#destdetail").hide();
	$("#holydetail").hide();
	$("#citylist").empty();
	$("#destlist").empty();
	$("#holylist").empty();
	$.ajax({
		type: 'POST',
		url: '/diving-back/product/cityJson.do',
		success: function(msg){
			if(msg==""||msg==null){$("#coudetail").hide();}
			
			else if(msg!=""||msg!=null){
				for(var i=0;i<msg.length;i++){
					$("#citylist").append("<li onclick='setcountry(this)' id="+msg[i].country_ID+">"+msg[i].country_Name/*+" "+msg[i].Country_Name_En*/+"</li>");
				}
				$("#coudetail").show();
			}
		}
	});
 }
 function findCountrys(){
	 $("#destdetail").hide();
	 $("#holydetail").hide();
	 $("#citylist").empty();
	 $("#destlist").empty();
	 $("#holylist").empty();
	 $.ajax({
		 type: 'POST',
		 url: '/diving-back/product/cityJson.do',
		 success: function(msg){			 
			if(msg!=""||msg!=null){
				 for(var i=0;i<msg.length;i++){
					 $('#citylist').append("<option id="+msg[i].country_ID+" value='"+msg[i].country_ID+"'>"+msg[i].country_Name+"</option>");
				 }
				 $("#coudetail").show();
			 }
		 }
	 });
 }
 
 function setcountry(obj){
	 $("#DESTINATION1").val(obj.innerHTML);
	 $("#country_id").val(obj.id);
	 
		$("#dest_id").val("");
		$("#holyland_id").val("");
		$("#DESTINATION2").val("");
		$("#DESTINATION3").val("");
		$("#SHIPNUM1").val("");
		$("#citylist").empty();
		$("#shiplist").empty();
		$("#shopdetail").val("");
		$("#shoplist").val("");
		$("#coudetail").hide();
		$("#destdetail").hide();
		$("#holydetail").hide();
		
		findShip();
}
 
// 查找目的地信息 
function findDesitination(){
	$("#destlist").empty();
	$("#holylist").empty();
	$("#DESTINATION3").val("");
	var countryID=$("#country_id").val();
	$.ajax({
		type: 'POST',
		url: '/diving-back/product/destJson.do',
		data: {countryID:countryID},
		success: function(msg){
			if(msg==""||msg==null){$("#destdetail").hide();}
			
			else if(msg!=""||msg!=null){
				for(var i=0;i<msg.length;i++){
					$("#destlist").append("<li onclick='setdest(this)' id="+msg[i].dest_ID+">"+msg[i].dest_Name/*+" "+msg[i].Dest_Name_en*/+"</li>");
				}
				
				$("#destdetail").show();
			}
		}
	});
}
// 查找目的地信息 
function findDesitinations(){
	$("#destlist").empty();
	$("#holylist").empty();
	$("#DESTINATION3").val("");
	var countryID=$("#country_id").val();
	$.ajax({
		type: 'POST',
		url: '/diving-back/product/destJson.do',
		data: {countryID:countryID},
		success: function(msg){
			if(msg!=""||msg!=null){
				for(var i=0;i<msg.length;i++){
					$('#destlist').append("<option id="+msg[i].dest_ID+" value='"+msg[i].dest_ID+"'>"+msg[i].dest_Name+"</option>");
				}
			}
		}
	});
}

function setdest(obj){
	$("#holyland_id").val("");
	$("#DESTINATION2").val(obj.innerHTML);
	$("#DESTINATION3").val("");
	$("#dest_id").val(obj.id);
	$("#SHOPNAME1").val("");
	$("#shopdetail").val("");
	$("#shoplist").val("");
	$("#destlist").empty();
	$("#SHOPNAME1").val("");
	$("#SHOPNAME").val("");
	$("#destdetail").hide();
	$("#holydetail").hide();
}
 
 // 查找圣地信息
 function findHolyland(){
	 $("#holylist").empty();
		var countryID = $("#country_id").val();
		var destId = $("#dest_id").val();
		$.ajax({
			type: 'POST',
			url: '/diving-back/product/holylandJson.do',
			data: {countryID : countryID,destId : destId},
			success: function(msg){
				if(msg==""||msg==null){$("#holydetail").hide();}
				else if(msg!=""||msg!=null){
					for(var i=0;i<msg.length;i++){
						$("#holylist").append("<li onclick='setholyland(this)' id="+msg[i].holyland_ID+">"+msg[i].holyland_Name/*+" "+msg[i].Holyland_Name_en*/+"</li>");
					}
					$("#holydetail").show();
				}
			}
		});
 }
 // 查找圣地信息
 function findHolylands(){
	 $("#holylist").empty();
	 var countryID = $("#country_id").val();
	 var destId = $("#dest_id").val();
	 $.ajax({
		 type: 'POST',
		 url: '/diving-back/product/holylandJson.do',
		 data: {countryID : countryID,destId : destId},
		 success: function(msg){
			 if(msg!=""||msg!=null){
				 for(var i=0;i<msg.length;i++){
					 $('#holylist').append("<option id="+msg[i].holyland_ID+" value='"+msg[i].holyland_ID+"'>"+msg[i].holyland_Name+"</option>");
				 }
			 }
		 }
	 });
 }
 
 function setholyland(obj){
	$("#DESTINATION3").val(obj.innerHTML);
	$("#holyland_id").val(obj.id);
	$("#holylist").empty();
	$("#TEACHER1").val("");
	$("#TEACHER").val("");
	$("#tealist").empty();
	$("#shopdetail").val("");
	$("#shoplist").val("");
	$("#holydetail").hide();
	// 圣地选择完成查询潜水店铺信息
	findShop();
}
 
 var bsSuggest ;
 // 查询潜水店铺信息
 function findShop(){
	 if (bsSuggest != undefined) {
		 $('#shopId').val('').attr('data-id','');
		 $("#shopId").bsSuggest('destroy');
	 }
	 var countryID = $("#country_id").val();
	 var destId = $("#dest_id").val();
	 var holyland_ID = $("#holyland_id").val();
	 $.ajax({
		type: 'POST',
		url: '/diving-back/product/shopJson.do',
		data:{countryID : countryID, destId : destId, holyland_ID : holyland_ID},
		success: function(msg){
			if(msg!="" || msg!=null){
				$("#shopId").empty();
				var ars = [];
				/*for(var i=0;i<msg.length;i++){
					$("#shopId").append('<option value="' + msg[i].center_ID + '">' + msg[i].center_Name+" "+msg[i].center_Name_En + '</option>');
				}*/
				//$('#shopId').chosen();
				
				for(var i = 0 ; i < msg.length; i++) {
					var cenName = "";
					if(msg[i].center_Name != '' ) {
						cenName = msg[i].center_Name;
					} if (msg[i].center_Name_En != '' ) {
						cenName = cenName + " " + msg[i].center_Name_En;
					}
					var obj = {};
					obj.centerID = msg[i].center_ID;
					obj.centerName = cenName;
					ars.push(obj);
				}
				bsSuggest = $("#shopId").bsSuggest({
				      indexId: 0,
				      indexKey: 1,
				      showBtn:false,
				      effectiveFields:["centerName"],
				      data:{"value":ars},
				      listStyle: {
				          "padding-top":0, "max-height": "375px","min-width":"600px",
				          "overflow": "auto",
				          "transition": "0.5s", "-webkit-transition": "0.5s", "-moz-transition": "0.5s", "-o-transition": "0.5s"
				      }
				 });
				/*var w = $("#shopId").outerWidth();
				setTimeout(function () {
					$('.dropdown-menu-right').css('min-width',w + 'px');
					$('.dropdown-menu-right').css('width',w + 'px');
				},1000)*/
			}
		}
	});
 }
 
 // 查询船号信息
 function findShip(idx){
	 var countryID = $("#country_id").val();
	 $.ajax({
		type: 'POST',
		url: '/diving-back/product/shipJson.do',
		data:{countryID : countryID},
		success: function(msg){
			if(msg!=""||msg!=null){
				$("#shipNum").empty();
				for(var i=0;i<msg.length;i++){
					if(idx == msg[i].ship_id) {
						$("#shipNum").append('<option value="' + msg[i].ship_id + '" selected="true">' + msg[i].ship_name+" "+msg[i].ship_name_en + '</option>');
					}else{
						$("#shipNum").append('<option value="' + msg[i].ship_id + '">' + msg[i].ship_name+" "+msg[i].ship_name_en + '</option>');
					}
					//$("#shipNum").append('<option value="' + msg[i].ship_id + '">' + msg[i].ship_name+" "+msg[i].ship_name_en + '</option>');
				}
				
			}
		}
	});
 }
 
 // 查询汇率信息
 function findCurrency(idx){
	 $.ajax({
		type: 'POST',
		url: '/diving-back/product/currencyJson.do',
		success: function(msg){
			if(msg!=""||msg!=null){
				$("#currency").empty();
				var currList = msg.aaData;
				for(var i=0; i<currList.length; i++){
					
					if(idx == currList[i].ID) {
						$("#currency").append('<option value="' + currList[i].ID + '" selected="true">' + currList[i].fromlabel+" "+currList[i].From + '</option>');
					}else{
						$("#currency").append('<option value="' + currList[i].ID + '">' + currList[i].fromlabel+" "+currList[i].From + '</option>');
					}
					//$("#currency").append('<option value="' + msg[i].from + '">' + msg[i].ship_name+" "+msg[i].ship_name_en + '</option>');
				}
				//$('#shopId').chosen();
			}
		}
	});
 }
 
 
var c1=3;
var c2=4;
//$("#bh").click(function(){
function bhClick(){
	c1=c1+2;
	
	var html = document.createElement("input");
	html.name="PRICEDESC"+c1;
	html.placeholder="请描述该费用项目";
	html.type="text";
	/* var html1 = document.createElement("input");
	html1.name="PRICE"+c1;
	html1.placeholder="请填写费用（单位￥：元）";	  
	html1.type="text"; */

	var html2 = document.createElement("div");
	html2.innerHTML = "删除该费用";
	html2.setAttribute("onclick","delroom(this)");
	html2.setAttribute("class","divbtn active");
	
	var newline= document.createElement("br"); 
	
	var row   = document.createElement("tr");
	var cell  = document.createElement("td");
	var cell1 = document.createElement("td");
	var cell2 = document.createElement("td");
	var cell3 = document.createElement("td");
	document.getElementById("bhfyid").appendChild(row);
	row.appendChild(cell2);
	row.appendChild(cell);
	row.appendChild(cell1);
	row.appendChild(cell3);
	cell.appendChild(html);
	cell1.appendChild(html1);
	cell3.appendChild(html2);
//});
}

//$("#ew").click(function(){
function ewClick(){
	c2=c2+2;
	
	var html = document.createElement("input");
	html.name="PRICEDESC"+c2;
	html.placeholder="请描述该费用项目";
	html.type="text";
	/* var html1 = document.createElement("input");
	html1.name="PRICE"+c2;
	html1.placeholder="请填写费用（单位￥：元）";	 
	html1.type="text"; */
		 
	var html2 = document.createElement("div");
	html2.innerHTML = "删除该费用";
	html2.setAttribute("onclick","delroom(this)");
	html2.setAttribute("class","divbtn active");
	
	var newline= document.createElement("br"); 
	
	var row   = document.createElement("tr");
	var cell  = document.createElement("td");
	var cell1 = document.createElement("td");
	var cell2 = document.createElement("td");
	var cell3 = document.createElement("td");
	document.getElementById("ewfyid").appendChild(row);
	row.appendChild(cell2);
	row.appendChild(cell);
	row.appendChild(cell1);
	row.appendChild(cell3);
	
	cell.appendChild(html);
	cell1.appendChild(html1);
	cell3.appendChild(html2);
//});
}

function delroom(obj){
	 var alssname = $(obj).attr('class');
	 $(obj).parent().parent().remove();
}

function delplans(obj){
	 $(obj).parents('table').remove();
}

function includeMon(id) {
	var includeMoney = "";
	$('#'+id+' tr').each(function() {
		var text = $(this).find('td').eq(1).find('input[type="text"]').val();
		if (text != undefined && text != '') {
			includeMoney += text + ",";
		}
	});
	includeMoney = includeMoney.substring(0,includeMoney.length - 1);
	return includeMoney;
}
 
function showpay(){
	$("#parmon").addClass("active");
	$("#allmon").removeClass("active");
	$("#divpayway").show();
	$("#payWay").val("定金");
}

function hidepay(){
	$("#allmon").addClass("active");
	$("#parmon").removeClass("active");
	$("#divpayway").hide();
	$("#payWay").val("全款");
}

/*function rmb(){
	$("#remb").addClass("active");
	$("#meiyuan").removeClass("active");
	$("#priceUnit").val("CNY");
}

function meiy(){
	$("#meiyuan").addClass("active");
	$("#remb").removeClass("active");
	$("#priceUnit").val("USD");
}*/

function canbuy(){
	$("#ohno").removeClass("active");
	$("#yesbuy").addClass("active");
	$("#proPublish").val("Y");
}

function nobuy(){
	$("#ohno").addClass("active");
	$("#yesbuy").removeClass("active");
	$("#proPublish").val("N");
}

/**
 * 1、套餐; 2、课程; 3、船宿 ;4、fundiving ;5、行程
 */
 
 function addMeal() {
	 window.location.href = "./mealAdd.html";
 }
 
 function addCourse() {
	 window.location.href = "./courseAdd.html";
 }
 
 function addFun() {
	 window.location.href = "./fundivingAdd.html";
 }
 
 function addShip() {
	 window.location.href = "./shipAdd.html";
 }
 
 function addTravel() {
	 window.location.href = "./travelAdd.html";
 }
 
 // 图片删除  type 为1表示封面图片删除,type为2表示轮播图删除
 function pictureDel(obj,type) {
	layer.confirm('确认删除吗？', {
		  btn: ['确定','取消'] //按钮
	}, function(){
		// 图片删除成功,如果是轮播图的删除则重新封装ID字符串,否则清楚封面图路径
		if (type == 1) {
			$("#firstPic").val("");
			$("#firstPath").val("");
			layer.msg("删除成功", {icon: 1});
			$(obj).remove();
		} else if (type == 2) {
			var id = $(obj).attr("data-id");
			$.ajax({
		       type : "POST",
		       url : '/diving-back/product/pictureDel.do',
			   data :{id : id},
		       dataType : "json",
		       success: function(msg){
		           if(msg != null && msg != "") {
		        	   if (msg) {
		        		   layer.msg("删除成功", {icon: 1});
		        		   // 重新组装轮播图ID字符串
		       		    	var pids = $("#pictures").val();
		          			var pid = id + ",";
		          			pids = pids.replace(pid,"");
		          			$("#pictures").val(pids);
		          			layer.msg("删除成功", {icon: 1});
		          			$(obj).remove();
		        		   
		        	   } else {
		        		   layer.msg("删除失败", {icon: 2});
		        	   }
		        	   
		           }
		       }
			});
		} else {
			console.log(3);
		}
	});
 }

// 目的地管理图片删除
function pictureDelPath(obj,flag){
	layer.confirm('确认删除吗？', {
		  btn: ['确定','取消'] //按钮
	}, function() {
		// 根据flag动态删除图片信息
		$("#pic" + flag).val("");
		layer.msg("删除成功", {icon: 1});
		$(obj).remove();
	});
}

function proCancelBtn(returnType){
	var module = getURLParams('module');
	if (module == undefined || module == null || module == '') {
		window.location.href = '/diving-back/app/product/productList.html?returnType='+returnType;
	} else {
		var status = getURLParams('status');
		// 审核管理跳转进来的
		window.location.href = '/diving-back/app/product/productApproval.html?status='+status;
	}
	
}
 
 